package hu.tortavar2.client.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import hu.tortavar2.client.ClientFactory;
import hu.tortavar2.client.SiteService;
import hu.tortavar2.client.SiteServiceAsync;
import hu.tortavar2.client.place.ManageSiteDataPlace;
import hu.tortavar2.client.view.adminpages.ManageSiteData;
import hu.tortavar2.client.view.adminpages.ManageSiteDataPageImpl;
import hu.tortavar2.shared.ProductCategory;
import hu.tortavar2.shared.Shop;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ManageSiteDataActivity extends AbstractActivity implements ManageSiteData.Presenter{

	private final ManageSiteData view;
	private final PlaceController placeController;
	
	private static final Logger log = Logger.getLogger(ManageSiteDataActivity.class.getName());
	SiteServiceAsync siteService = GWT.create(SiteService.class);
	
	public ManageSiteDataActivity(ManageSiteDataPlace place, ClientFactory clientFactory) {
		view = new ManageSiteDataPageImpl();
		placeController = clientFactory.getPlaceController();
		refreshShops();
		}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setPresenter(this);
		panel.setWidget(view.asWidget());
	}

	@Override
	public void onAddNewCategory(ProductCategory category) {
		
	}
	
	@Override
	public void onDeleteCategory(String encodedKey) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAddNewImage() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAddNewShop(Shop shop) {
		// TODO Auto-generated method stub
		siteService.addShop(shop, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public List<Shop> refreshShops() {
		
		siteService.getShops(new AsyncCallback<List<Shop>>() {
			@Override
			public void onSuccess(List<Shop> result) {
				// TODO Auto-generated method stub
				view.setShops(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
		return null;
	}
}
